package com.cgc999.host.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cgc999.host.R;
import com.cgc999.host.adapter.PromoteListAdapter;
import com.cgc999.host.base.BaseLazyFragment;
import com.cgc999.host.api.PromoteService;
import com.cgc999.host.view.CgcItemDecoration;
import com.cgc999.host.vo.PageResult;
import com.cgc999.host.vo.PromoteInfoVo;
import com.cgc999.host.vo.ResponseInfo;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PromoteListFragment extends BaseLazyFragment {

    private final String TAG="PromoteList";

    private XRecyclerView xRecyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        initRecycler(view);
        return view;
    }

    private List<PromoteInfoVo> list;
    public void initRecycler(View view) {
        xRecyclerView = view.findViewById(R.id.listfragment_xrecyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);
        list = new ArrayList<>();
        getData();
        xRecyclerView.addItemDecoration(new CgcItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                xRecyclerView.loadMoreComplete();
            }
        });
    }


    private void getData(){
        Retrofit retrofit2 = new Retrofit.Builder()
                .baseUrl("http://192.168.10.121:18011")
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient())
                .build();

        PromoteService iInfo = retrofit2.create(PromoteService.class);
        String json="{   \n" +
                "\t\"open_id\": \"apUser180839080002\",\n" +
                "\t\"cur_page\":0\n" +
                "}";
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        Call<ResponseInfo<PageResult<PromoteInfoVo>>> call = iInfo.gainPromoteList(body);

        call.enqueue(new Callback<ResponseInfo<PageResult<PromoteInfoVo>>>() {
            @Override
            public void onResponse(Call<ResponseInfo<PageResult<PromoteInfoVo>>> call, Response<ResponseInfo<PageResult<PromoteInfoVo>>> response) {
                //成功获取公众号列表信息
                ResponseInfo<PageResult<PromoteInfoVo>> promoteInfos = response.body();
                Log.i(TAG,"promoteInfos.getMsg(): " + promoteInfos.toString());
                list = promoteInfos.getData().getList();
                xRecyclerView.setAdapter(new PromoteListAdapter(getActivity(), list));

            }

            @Override
            public void onFailure(Call<ResponseInfo<PageResult<PromoteInfoVo>>> call, Throwable t) {
                Log.i(TAG,"onFailure: " + t.getMessage());
            }
        });
    }

    @Override
    public void reFreshData() {

    }


//    private void getData() {
//        RequestParams params = new RequestParams("http://192.168.10.121:18011/promote/gainPromoteList");
//        params.addHeader("Content-Type","application/json");
////        params.setBodyContentType("application/json");
////        RequestParams params = new RequestParams("https://www.baidu.com:80");
//        params.addBodyParameter("open_id","apUser180839080002");
//        params.addBodyParameter("cur_page","0");
//        x.http().post(params, new Callback.CommonCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                Log.e("onSuccess----->", result);
//                Gson gson = new Gson();
//                Bean bean = gson.fromJson(result, Bean.class);
//                Toast.makeText(getActivity(), "走到这里了 bean是空的吗？" + bean.data.get(0).imagePath, Toast.LENGTH_SHORT).show();
//                list = bean.data;
//                xRecyclerView.setAdapter(new PromoteListAdapter(getActivity(), list));
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//                Log.e(TAG, "onError:"+ex.getMessage());
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//                Log.e(TAG, "onCancelled");
//            }
//
//            @Override
//            public void onFinished() {
//                Log.e(TAG, "onFinished");
//            }
//        });
//    }
}
